<template>
  <div class="navBox" ref="navBox">
    <div class="navlogo"
      style="display: flex;flex-direction: column;justify-content: center;align-items: center;padding: 15px 0 5px 0;"><img
        style="width: 70px;height: 30px;" src="@/assets/logo_1.png" alt="">
      <span>Backend Management System</span>
    </div>
    <el-menu ref="nav" :default-active="defaultActive" class="el-menu-vertical-demo custom-menu" @open="handleOpen"
      @close="handleClose" :background-color="colornav" :text-color="textcolor" :collapse="isCollapse" @select="clickNva"
      :active-text-color="oncolor">
      <el-submenu v-for="(item, index) in menuData" :key="index" :index="item.menu_id + ''">
        <template slot="title">
          <i :class="getIcon(item)"></i>
          <span>{{ item.name }}</span>
        </template>
        <el-menu-item-group v-for="(item2, index2) in item.childs" :key="index2 + '10086'">
          <el-menu-item :index="item.menu_id + '-' + item2.menu_id" class="caidan" @click="clickMenuItem(item2)">{{
            item2.name
          }}</el-menu-item>
        </el-menu-item-group>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
// import { menu, permissions } from "../../utils/api";
import { menuBar } from "../../utils/api";
export default {
  name: "navInfo",
  data() {
    return {
      onoffcolor: "background: #ccc",
      oncolor: "#409EFF",
      textcolor: "#fff",
      colornav: "#2a2f43",
      isCollapse: false,
      classType: "el-icon-arrow-right",
      menuData: [],
      defaultActive: '1-2', // 设置默认激活项
    };
  },
  created() {
  },
  props: {
    userData: {
      required: true,
    },
  },
  mounted() {
    this.getMenu()
  },
  methods: {
    getIcon(item) {
      // 主导航图标筛选
      // 1.管理员
      // 3.用户
      // 6.内容
      // 12.股票管理
      // 16.支付
      // 19.爬虫
      // 21.系统
      const iconMap = new Map([
        [1, 'el-icon-s-custom'],
        [3, 'el-icon-user-solid'],
        [6, 'el-icon-s-grid'],
        [12, 'el-icon-money'],
        [16, 'el-icon-bank-card'],
        [19, 'el-icon-document-copy'],
        [21, 'el-icon-s-tools'],
      ]);
      return iconMap.get(item.menu_id) || 'el-icon-s-grid'; // 默认图标
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    clickNva(index) {
      this.$emit("child-clicked", index);
    },
    clickMenuItem(item) {
      console.log('Clicked menu item:', item);
    },
    async getMenu() {
      const res = await menuBar().catch(err => {
        return console.error(err)
      })
      if (res.data && res.status == '200') {
        this.$message.success(res.data.message)
        this.menuData = res.data.data
        // this.menuData.push({
        //   menu_id: 888, name: '测试', level: 1, childs: [{
        //     menu_id: 999,
        //     name: '测试页面',
        //   }]
        // })
      } else {
        this.$message.error(res.data.message)

      }
    }
  },
};
</script>

<style lang="less" scoped>
.caidan {
  // text-align: right;
  padding-left: 60px !important;
}

.navBox {
  background-color: #2a2f43;
  height: 100vh;
  line-height: 0;
  overflow-y: auto;

  .navlogo {
    width: 100%;
    color: #fff;
    font-size: 16px;
    line-height: 50px;
  }

  .Collapse {
    position: absolute;
    left: 0;
    display: inline-block;
    padding: 10px 20px;
    font-size: 16px;
    font-weight: bold;
    text-align: center;
    text-decoration: none;
    color: #ffffff;
    border-radius: 5px;
    border: none;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
    cursor: pointer;
    width: 65px;
  }

  .custom-button:hover {
    background: linear-gradient(to right, #ff4b2b, #ff7f50);
  }
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 280px;
  // min-height: 400px;
}

.el-menu-vertical-demo {
  border: none !important;
}
</style>
<style>
.el-menu-item-group__title {
  display: none !important;
}

.el-table__header-wrapper {
  line-height: 0;
}

.custom-menu {
  justify-content: flex-start;
  /* 内容左对齐 */
}

.el-submenu .el-menu-item {
  text-align: left;
}

.el-submenu__title {
  text-align: left;
  padding-left: 80px;
}
</style>
